import { useUserStore } from '@/stores/user'

export const authGuard = (to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth && !userStore.token) {
    next('/login')
  } else if (to.meta.requiresAuth && to.meta.role && to.meta.role !== (userStore.isAdmin ? 'admin' : 'user')) {
    next(userStore.isAdmin ? '/admin' : '/user')
  } else {
    next()
  }
}
